package com.joshua.spring.util;

import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * projectName dragon-cloud
 * package com.joshua.spring.util
 * classname  KeyUtils
 * <p>
 * description
 * </p>
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/9/6 15:35
 */
public class KeyUtils {
    public static String getKeyWithDate(String prefix) {
        prefix = StringUtils.isEmpty(prefix) ? "" : prefix;
        String uuid = UUID.randomUUID().toString()
                .replace("-", "").substring(0, 5);
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        return String.format("%s%s-%s", prefix, uuid, formatter.format(dateTime));
    }

    public static String defaultKeyGen() {
        long currentTime = System.currentTimeMillis();
        String uuid = UUID.randomUUID().toString().substring(1, 2);
        StringBuffer buffer = new StringBuffer(uuid);
        return buffer.append(currentTime).toString();
    }
}
